#ifndef STX_ALGORITHM_SHUFFLE_HPP_
#define STX_ALGORITHM_SHUFFLE_HPP_

#include <stx/algorithm/common.hpp>
#include <stx/algorithm/random_shuffle.hpp>
#include <stx/algorithm/swap.hpp>

namespace stx {

template<class RandomAccessIterator, class UniformRandomNumberGenerator>
inline void
shuffle(
    RandomAccessIterator first,
    RandomAccessIterator last,
#ifdef STX_HAS_RVALUE_REFERENCES
    UniformRandomNumberGenerator&& g)
#else
    UniformRandomNumberGenerator& g)
#endif
{
    typedef typename std::iterator_traits<RandomAccessIterator>::difference_type difference_type;
    difference_type d = last - first;
    if (d > 1) {
        for (--last, --d; first < last; ++first, --d) {
            difference_type i = random_number_impl(d);
            if (i != difference_type(0)) {
                swap(*first, *(first + i));
            }
        }
    }
}

} // namespace stx

#endif // STX_ALGORITHM_SHUFFLE_HPP_
